var app = getApp();
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    xturl: app.myUlife.ulifeUrl + "/",
    czjl: app.myUlife.ulifeUrl + '/wx/title_script_img.png',//成长记录字
    username: '',//姓名
    avatarUrl: '',//我的头像
    gender: '',
    slide: app.myUlife.ulifeUrl + '/wx/slide_btn_icon.png',//默认图片
    data_bg_img: app.myUlife.ulifeUrl + '/wx/data_bg_img.jpg',//成长记录

    //滑动
    scrollindex: 0,
    totalnum: 2,  //总共页面数
    starty: 0,  //开始的位置x 
    endy: 0, //结束的位置y
    margintop: 0,  //滑动下拉距离

    planlog:0,//我阅读的书籍总数
    plog: 0,//我的诗词完成任务数
    hchjhd: 0,//好词好句号段数
    zjdhg: 0,//章节读后感

    //框
    k1: app.myUlife.ulifeUrl + '/kuang/k1.png',
    k2: app.myUlife.ulifeUrl + '/kuang/k2.png',
    k3: app.myUlife.ulifeUrl + '/kuang/k3.png',
    k4: app.myUlife.ulifeUrl + '/kuang/k4.png',
    k5: app.myUlife.ulifeUrl + '/kuang/k5.png',
    k_bg_img: app.myUlife.ulifeUrl + '/kuang/k_bg_img.jpg',


    showsqModalStatus: false,//提示弹出
  },

  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
    var usid = wx.getStorageSync('usid');
    if (!usid) {
      wx.reLaunch({
        url: '../../pages/login/login'
      })
      return;
    }

    var gender = wx.getStorageSync('gender');
    this.setData({
      gender: gender,
      avatarUrl: wx.getStorageSync('avatarUrl'),
      username: wx.getStorageSync('name')
    });
    //获取统计
    this.getMyCzStatic();
  },

  //获取我的成长记录
  getMyCzStatic(){
    var that=this;
    var usid = wx.getStorageSync('usid');
    wx.request({
      url: app.myUlife.ulifeUrl + 'staticlog/getczjl.html',
      data: {
        usid: usid
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        that.setData({
          planlog: res.data.planlog,//我阅读的书籍总数
          countword: res.data.countword,//我阅读的书籍的字数统计
          plog: res.data.plog,//我的诗词完成任务数 
          hchjhd: res.data.hchjhd,//我的阅读记录
          zjdhg: res.data.zjdhg,//章节读后感
          ydjy: res.data.ydjy,//阅读剪影
        })
      }
    })
  },

  //滑动开始 start
  scrollTouchstart: function (e) {
    let py = e.touches[0].pageY;
    this.setData({
      starty: py
    })
  }, 
  scrollTouchmove: function (e) {
    let py = e.touches[0].pageY;
    let d = this.data;
    this.setData({
      endy: py,
    })
    if (py - d.starty < 100 && py - d.starty > -100) {
      this.setData({
        margintop: py - d.starty
      })
    }
  },
  scrollTouchend: function (e) {
    let d = this.data;
    if (d.endy - d.starty > 100 && d.scrollindex > 0) {
      this.setData({
        scrollindex: d.scrollindex - 1
      })
    } else if (d.endy - d.starty < -100 && d.scrollindex < this.data.totalnum - 1) {
      this.setData({
        scrollindex: d.scrollindex + 1
      })
    }
    this.setData({
      starty: 0,
      endy: 0,
      margintop: 0
    })
  },
  //滑动开始 end


  gotoreadlog(){
    wx.navigateTo({
      url: '../readlog/readlog'
    })
  },


  gotopoetrylog() {
    var plog = this.data.plog;
    if (plog==0){
      wx.showModal({
        title: '失败',
        content: "没有任何诗词阅读记录",
        showCancel: !1,
      });
      return;
    }
    wx.navigateTo({
      url: '../poetryreadlog/poetryreadlog'
    })
  }, 

  onGotUserInfo(e) {
    wx.removeStorageSync('city');
    wx.removeStorageSync('province');
    wx.removeStorageSync('gender');
    wx.removeStorageSync('nickName');
    wx.removeStorageSync('avatarUrl');

    //加入缓存
    var userInfo = e.detail.userInfo;
    wx.setStorageSync('city', userInfo.city);
    wx.setStorageSync('province', userInfo.province);
    wx.setStorageSync('gender', userInfo.gender);
    wx.setStorageSync('nickName', userInfo.nickName);
    wx.setStorageSync('avatarUrl', userInfo.avatarUrl);

    this.setData({
      gender: userInfo.gender,
      avatarUrl: userInfo.avatarUrl,
    });
  },
















  //...................授权获取用户基本信息......................
  opensqdl() {
    var gender = this.data.gender;
    if (gender == '未授权登录') {
      this.qrsqopen("open");
    }
  },
  qrsqShow(e) {
    var currentStatu = e.currentTarget.dataset.statu;
    this.qrsqopen(currentStatu);
  },
  cancelopen() {
    this.qrsqopen("close");
  },
  qrsqopen: function (currentStatu) {
    /* 动画部分 */
    // 第1步：创建动画实例   
    var animation = wx.createAnimation({
      duration: 200, //动画时长  
      timingFunction: "linear", //线性  
      delay: 0 //0则不延迟  
    });

    // 第2步：这个动画实例赋给当前的动画实例  
    this.animation = animation;

    // 第3步：执行第一组动画  
    animation.opacity(0).rotateX(-100).step();

    // 第4步：导出动画对象赋给数据对象储存  
    this.setData({
      animationData: animation.export()
    })

    // 第5步：设置定时器到指定时候后，执行第二组动画  
    setTimeout(function () {
      // 执行第二组动画  
      animation.opacity(1).rotateX(0).step();
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象  
      this.setData({
        animationData: animation
      })
      //关闭  
      if (currentStatu == "close") {
        this.setData({
          showsqModalStatus: false
        });
      }
    }.bind(this), 200)
    // 显示  
    if (currentStatu == "open") {
      this.setData({
        showsqModalStatus: true
      });
    }
  },


  onGotUserInfo(e) {
    wx.removeStorageSync('city');
    wx.removeStorageSync('province');
    wx.removeStorageSync('gender');
    wx.removeStorageSync('nickName');
    wx.removeStorageSync('avatarUrl');

    //加入缓存
    var userInfo = e.detail.userInfo;
    wx.setStorageSync('city', userInfo.city);
    wx.setStorageSync('province', userInfo.province);
    wx.setStorageSync('gender', userInfo.gender);
    wx.setStorageSync('nickName', userInfo.nickName);
    wx.setStorageSync('avatarUrl', userInfo.avatarUrl);

    this.setData({
      gender: userInfo.gender,
      avatarUrl: userInfo.avatarUrl,
    });
    this.qrsqopen("close");
  },
//...................授权获取用户基本信息end......................


})